package com.example.musicplayer.activity;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.musicplayer.R;
import com.example.musicplayer.model.Video;
import com.example.musicplayer.mycontroller.VideoController;
import com.example.musicplayer.util.DataSaveLoad;
import com.example.musicplayer.util.ImageUtil;
import com.example.musicplayer.util.MediaListUtil;


public class VideoListActivity extends Activity
{
	
	ListView videoListView;//视频列表视图
	VideoController videoController;//视频控制器
	Video video;//视频播放对象
	AlertDialog alertDialog;//新建AlertDialog
	ArrayList list=null;//新建ArrayList
	boolean isScroll=true;//新建boolean对象
	
	//初始化参数
	public void init()
	{
		video = Video.getInstance();
		video.reCreate();
		videoController = VideoController.getInstance(video);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setTheme(android.R.style.Theme_Holo_Wallpaper);//设置主题
		setContentView(R.layout.video_list);//设置布局
		videoListView=(ListView)findViewById(R.id.videoListView);//通过Id查找控件
		//设置列表滚动监听
		videoListView.setOnScrollListener(new AbsListView.OnScrollListener(){

				@Override
				public void onScrollStateChanged(AbsListView p1, int p2)
				{
				}

				@Override
				public void onScroll(AbsListView p1, int p2, int p3, int p4)
				{
					isScroll=true;
				}
			});
		//设置Item选择监听
		videoListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					Intent intent=new Intent(VideoListActivity.this,VideoActivity.class);//新建Intent
					intent.putExtra("videoIndex",p3);//传入参数
					startActivity(intent);//跳转
				}
			});
		init();
		load();
		new ImageThread().start();
	}
	
	/**
	 * 	视频略缩图加载
	 * @author hyh
	 *
	 */
	class ImageThread extends Thread
	{

		@Override
		public void run()
		{
			while(true){
				if(isScroll){
					try{
						int start=videoListView.getFirstVisiblePosition();//获取列表开始项
						int count=videoListView.getChildCount();//获取界面界面显示的列表项
						for(int i=start;i<start+count;i++){
							String path=videoController.getVideoPath(i);//获取视频路径
							String imagePath= ImageUtil.getStringBitmap(path,300,300);//获取视频略缩图
							Message m=new Message();//新建Message对象
							m.obj=imagePath;//传入数据
							m.what=i;//传入数据
							handlerImage.sendMessage(m);//发送消息
						}
						isScroll=false;
					}catch(Exception e){
						
					}
				}
			}
			
		}
		
	}
	
	Handler handlerImage=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			try{
				String b=(String)msg.obj;//读取数据
				if(b!=null){
					int count=msg.what;//读取数据
					int start=videoListView.getFirstVisiblePosition();//获得界面开始项
					ImageView textView=(ImageView)videoListView.getChildAt(count-start).findViewById(R.id.video_itemImageView);//通过ID获取ImageView
					textView.setImageBitmap(BitmapFactory.decodeFile(b));//为ImageView设置图片
				}
			}catch(Exception e){
				
			}
			
			
		}
		
		
	};
	
	/**
	 * 获得视频列表
	 * @param args 不定项参数
	 * @return 视频列表
	 */
	public ArrayList getVideoList(String ...args){
	
		if (args.length > 0)
		{
			list = videoController.getVideoList(args[0]);
		}
		else
		{
			list = videoController.getVideoList();
		}
		return list;
	}
	

	/**
	 * 填充数据
	 * @param list 数据集合
	 */
	public void fillData(ArrayList list){
		
		String from[]={"videoImage","videoName","totalTime"};//新建String数组
		int to[]={R.id.video_itemImageView ,R.id.video_itemTextView1,R.id.video_itemTextView2};//新建int数组
		List<Map<String,String>> data=new ArrayList<Map<String,String>>();//新建List集合
		for(int i=0;i<list.size();i++){
			try{
				String videoName=(String)list.get(i);//获取视频名
				video.setPath(videoController.getVideoPath(i));//设置视频路径
				Date date=new Date(video.getTotalTime());//新建Date类
				Map map=new HashMap();//新建Map集合用于存储数据
				map.put("videoImage", R.drawable.ic_menu_camera_video_view);
				map.put("videoName", videoName);
				map.put("totalTime", "0"+video.getTotalTime()/1000/60/60+":"+String.format("%tM:%tS",date,date));
				data.add(map);
			}catch(Exception e){
				
			}
			
		}
		
		SimpleAdapter simpleAdapter=new SimpleAdapter(this, data, R.layout.video_item, from, to);//创建适配器
		videoListView.setAdapter(simpleAdapter);//设置适配器
	}
	
	//创建菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(Menu.NONE, 1, 0, "更新视频");
		return true;
	}


	//监听菜单
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		 if (item.getItemId() == 1){
			DataSaveLoad.delete("nameList"+ MediaListUtil.TYPE_VIDEO);
			DataSaveLoad.delete("pathList"+MediaListUtil.TYPE_VIDEO);
		    load();
		}
		return true;
	}

	/**
	 * 加载数据
	 * @param args 不定项参数
	 */
	public void load(String ...args){
		AlertDialog.Builder b=new AlertDialog.Builder(this);
		b.setMessage("正在更新中…");
		alertDialog=b.show();
		new RefreshVideoList(args).start();
	}
	
	//更新视频列表
	class RefreshVideoList extends Thread
	{
		String args[];
		
		public RefreshVideoList(String ...args){
			this.args=args;
		}
		@Override
		public void run()
		{
			ArrayList list=getVideoList(args);
			Message m=new Message();
			m.obj=list;
			h.sendMessage(m);
		}
	}

	//通过Handler加载数据
	Handler h=new Handler(){
		
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			ArrayList list=(ArrayList)msg.obj;
			fillData(list);
			alertDialog.dismiss();
		}

	};
	
}
